#include "SContentView.h"

#include "EditorStyleSet.h"
#include "SContentTextItem.h"
#include "Framework/Application/SlateApplication.h"
#include "HAL/PlatformApplicationMisc.h"
#include "HAL/PlatformProcess.h"
#include "Launch/Resources/Version.h"
#include "Widgets/Input/SHyperlink.h"

#include "SKeyValueRow.h"
#include "SuperClipBoardApp.h"

#if PLATFORM_WINDOWS
#include "Windows/WindowsPlatformApplicationMisc.h"
#include "Windows/WindowsPlatformTime.h"
#elif PLATFORM_LINUX
#include "Linux/LinuxPlatformApplicationMisc.h"
#include "Linux/LinuxPlatformTime.h"
#endif // PLATFORM_WINDOWS

DEFINE_LOG_CATEGORY(LogSContentView)

#define LOCTEXT_NAMESPACE "SContentView"

SContentView::SContentView()
{
	ContentVB = SNew(SVerticalBox);
}

SContentView::~SContentView()
{
}

FReply SContentView::OnTestItem()
{
	ContentVB->AddSlot()
	.AutoHeight()
	[
		SNew(SContentTextItem)
		.ContentText(TEXT("SContentView::OnTestItem"))
		.OnSelectionChanged(this, &SContentView::OnContentTextItemSelectionChanged)
	];
	return FReply::Handled();
}

FReply SContentView::OnCopy()
{
	ProcessCopy();

	return FReply::Handled();
}


void SContentView::ProcessCopy()
{
	FString CopyValue;
	int32 CopyedContentNum = 0;
	for (auto& Item : CopyContent)
	{
		CopyedContentNum++;
		CopyValue.Append(Item.Value);
		if (CopyedContentNum == CopyContent.Num())
			break;
		CopyValue += TEXT("\n");
	}
	bSelfCopy = true;
	FPlatformApplicationMisc::ClipboardCopy(*CopyValue);

	ClearCopy();
#if PLATFORM_LINUX
	uint32 CurPid = FPlatformProcess::GetCurrentProcessId();
	FSuperClipBoardApplication::WindowminimizebyPid(CurPid);
#elif PLATFORM_WINDOWS
	FSlateApplication::Get().GetActiveTopLevelWindow()->GetNativeWindow()->Minimize();
#endif
}

void SContentView::Construct(const FArguments& Args)
{
	StartTime = FPlatformTime::Seconds();
	ContentVB->AddSlot()
			.AutoHeight()
			[
				
				SNew(SHorizontalBox)
				/*+ SHorizontalBox::Slot()
				[
					SNew(SButton)
					.Text(LOCTEXT("TestItem", "TestItem"))
					.ClickMethod(EButtonClickMethod::MouseUp)
					.OnClicked(this, &SContentView::OnTestItem)
				]*/
				+SHorizontalBox::Slot()
				.HAlign(HAlign_Fill)
				.VAlign(VAlign_Fill)
				[
					SNew(SButton)
					.HAlign(HAlign_Center)
					.Text(LOCTEXT("Copy", "复制"))
					.ClickMethod(EButtonClickMethod::MouseUp)
					.OnClicked(this, &SContentView::OnCopy)
				]
			];
	ChildSlot
	[
			SNew(SBorder)
			.BorderImage(FEditorStyle::GetBrush("NotificationList.ItemBackground"))
			.Padding(FMargin(2.f,2.f))
			[
				ContentVB.ToSharedRef()
			]
	];

}

void SContentView::OnClipBoardStringChange(const FString& Content)
{
	if (!ContentVB.IsValid() || bSelfCopy || Content.IsEmpty())
	{
		bSelfCopy = false;
		return;
	}

	const TSharedPtr<SContentTextItem> ContentTextItem =
		SNew(SContentTextItem)
		.ContentText(Content)
		.OnSelectionChanged(this, &SContentView::OnContentTextItemSelectionChanged);

	ContentVB->InsertSlot(1)
		.AutoHeight()
		[
			ContentTextItem.ToSharedRef()
		];
	FSuperClipBoardApplication::InsertNewItem.Broadcast();
	ItemIndex++;
}

void SContentView::OnContentTextItemSelectionChanged(const bool bSelected,SContentTextItem* ContentTextItem, const FString& Content)
{
	UE_LOG(LogSContentView, Log, TEXT("OnContentTextItemSelectionChanged bSelected:%d; Content:%s"),bSelected, *Content);
	bool bContains = CopyContent.Contains(ContentTextItem);
	if (bSelected && !bContains)
	{
		CopyContent.Add(ContentTextItem, Content);
		return;
	}

	if (!bSelected && bContains)
	{
		CopyContent.Remove(ContentTextItem);
	}
}

void SContentView::ClearCopy()
{
	for (auto& Item : CopyContent)
	{
		SContentTextItem* ItemWidget = Item.Key;
		if (ItemWidget)
		{
			ItemWidget->SetDefaultColor();
		}
	}

	CopyContent.Empty();
}


FReply SContentView::OnKeyUp(const FGeometry& MyGeometry, const FKeyEvent& InKeyEvent)
{
	if (InKeyEvent.IsLeftControlDown() && InKeyEvent.GetKey() == FKey(TEXT("C")))
	{
		UE_LOG(LogSContentView, Log, TEXT("OnKeyUp Ctl+C"));
		ProcessCopy();
	}
	return FReply::Unhandled();
}

void SContentView::OnWindowDeactivated()
{
	UE_LOG(LogSContentView, Log, TEXT("SContentView::OnWindowDeactivated"));
	ClearCopy();
}

#undef LOCTEXT_NAMESPACE
